<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="消息标题" prop="msgTitle">
        <el-input
          v-model="queryParams.msgTitle"
          placeholder="消息标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送人" prop="accepterUser">
        <el-input
          v-model="queryParams.accepterUser"
          placeholder="发送人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="消息状态" prop="msgStatus">
        <el-select v-model="queryParams.msgStatus" placeholder="消息状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['Msg:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['Msg:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['Msg:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="msgList"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index"  :show-overflow-tooltip="false" />
      <el-table-column label="消息标题" align="center" prop="msgTitle" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <router-link :to="'/dict/type/data/' + scope.row.dictId" class="link-type">
            <span>{{ scope.row.msgTitle }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="发送人" align="center" prop="senderUser"  />
      <el-table-column label="发送时间" align="center" prop="creationDate" width="155"/>
      <el-table-column label="接收人" align="center" prop="accepterUser" />
      <el-table-column label="处理时间" align="center" prop="processingTime" width="155"/>
      <el-table-column  label="消息状态" align="center" prop="msgStatus" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding" width="110">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['Msg:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['Msg:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.rows"
      @pagination="getList"
    />

    <!-- 添加或修改消息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="880px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="消息标题" prop="msgTitle">
              <el-input v-model="form.msgTitle" placeholder="请输入消息标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接收人" prop="accepterNo">
              <el-select v-model="form.accepterNo" filterable placeholder="请选择">
                <el-option
                  v-for="user in userList"
                  :key="user.userNo"
                  :label="user.nickName"
                  :value="user.userNo"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="form.msgContent"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listMsg, getMsg, addMsg, updateMsg, handlerMsg,delMsg } from "@/api/common/msg";

  export default {
    name: "msg",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 消息表格数据
        msgList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 用户列表
        userList: [],
        // 日期范围
        dateRange: [],
        // 分页参数
        pageParams: {
          page: 1,
          rows: 10
        },
        // 查询参数
        queryParams: {
          msgTitle: undefined,
          accepterNo: undefined,
          accepterUser: undefined,
          msgStatus: undefined,
          searchValue: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          msgTitle: [
            { required: true, message: "消息标题不能为空", trigger: "blur" }
          ],
          accepterNo: [
            { required: true, message: "接收人不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
      /** 查询消息列表 */
      /*this.loading = true;
      this.queryParams.searchValue="all";
      listMsg(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.msgList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.queryParams.searchValue=undefined;
      });*/
      //查询消息字典状态
      this.getDicts("msg_status").then(response => {
        this.statusOptions = response.responseBody.result;
      });
      //查询所有用户
      // listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
      //     this.userList = response.rows;
      //     this.total = response.total;
      //     this.loading = false;
      //   }
      // );
    },
    methods: {
      /** 查询消息列表 */
      getList() {
        this.loading = true;
        listMsg({'pageParameter': this.pageParams, 'searchParams': this.queryParams}).then(response => {
          this.msgList = response.responseBody.result;
          this.total = response.responseBody.pageParameter.totalCount;
          this.loading = false;
        });
      },
      // 消息状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.msgStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          msgId: undefined,
          msgTitle: undefined,
          accepterNo: undefined,
          msgAccepter: undefined,
          msgStatus: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.msgId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加消息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const msgId = row.msgId || this.ids
        getMsg(msgId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改消息";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.msgId != undefined) {
              updateMsg(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMsg(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const msgIds = row.msgId || this.ids
        this.$confirm('确认删除消息编号为"' + msgIds + '"的数据项?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          delMsg(msgIds).then(response => {
            this.msgSuccess("删除成功");
            this.getList();
          })
        }).catch(() => {})
      }
    }
  };
</script>
